﻿using Finbook.ConsoleApp.Models;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Finbook.ConsoleApp.Data
{
    public class MongoTestContext
    {
        private readonly IMongoClient _mongoClient;
        private readonly IMongoDatabase _mongoDatabase;
        public MongoTestContext()
        {
            _mongoClient = new MongoClient("mongodb://140.143.88.100:27017");
            if (_mongoClient != null)
            {
                _mongoDatabase = _mongoClient.GetDatabase("test");
            }
        }

        public IMongoCollection<Person> Person
        {
            get
            {
                CheckAndCreateCollection(nameof(Person));
                var collection = _mongoDatabase.GetCollection<Person>(nameof(Person));
                return collection;
            }
        }

        private void CheckAndCreateCollection(string name)
        {
            var names = _mongoDatabase.ListCollectionNames().ToList();
            if (!names.Any(n => n.Equals(name)))
            {
                _mongoDatabase.CreateCollection(name);
            }
            
        }
    }
}
